package fr.android.activity;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import fr.android.R;
import fr.android.bo.Projet;
import fr.android.db.DataHelper;

public class Accueil extends Activity implements OnClickListener {

	private DataHelper dh;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		Button b = (Button) findViewById(R.id.nouveauProjet);
		b.setOnClickListener(this);

		Projet[] array_spinner = new Projet[5];
		array_spinner[0] = new Projet(1, "1");
		array_spinner[1] = new Projet(2, "2");
		array_spinner[2] = new Projet(3, "3");
		array_spinner[3] = new Projet(4, "4");
		array_spinner[4] = new Projet(5, "5");
		Spinner spinner = (Spinner) findViewById(R.id.listeProjets);
		ArrayAdapter<Projet> adapter = new ArrayAdapter<Projet>(this,
				android.R.layout.simple_spinner_item, array_spinner);
		spinner.setAdapter(adapter);

		TextView output = (TextView) this.findViewById(R.id.out_text);

		this.dh = new DataHelper(this);
//		this.dh.deleteAll();
		List<String> names = this.dh.selectAll();
		StringBuilder sb = new StringBuilder();
		sb.append("Names in database:\n");
		for (String name : names) {
			sb.append(name + "\n");
		}

		output.setText(sb.toString());

	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.nouveauProjet: {
			Object a = ((Spinner) findViewById(R.id.listeProjets))
					.getSelectedItem();
			System.out.println(a.toString());
			Intent it = new Intent(this, Nouveau.class);
			startActivity(it);
			break;
		}
		}
	}
}